package com.fdb.efp.esb.service.impl.zx;

import java.nio.charset.Charset;
import java.util.Base64;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import com.baidu.disconf.client.common.annotations.DisconfFile;
import com.baidu.disconf.client.common.annotations.DisconfFileItem;
import com.baidu.disconf.client.common.annotations.DisconfUpdateService;
import com.baidu.disconf.client.common.update.IDisconfUpdate;

/**
 * @description:征信接口的相关配置类
 * 注意：此类仅作为本模块下的组件，不对外部模块暴露
 * 注意：确保BaseZXService的子类在ZXConfig的bean被创建
 * 之后创建（在子类上使用@DependsOn注解显式声明）
 *
 * @date:2018年12月15日 上午10:29:52
 * @author:hengyh
 */
@Component("ZXConfig")
@DisconfFile(filename = "zx.properties")
@DisconfUpdateService(classes = ZXConfig.class)
public class ZXConfig implements InitializingBean, IDisconfUpdate {

	@Value("${zx.systemId}")
	private String systemId;
	//	@Value("${zx.userId}")
//	private String userId;
	@Value("${zx.key}")
	private String key;
	@Value("${zx.validDay}")
	private String validDay;

	//	private String pKey;
	private final Base64.Encoder encoder = Base64.getEncoder();

	/** 在属性值初始化完成之后执行该方法 **/
	@Override
	public void afterPropertiesSet() throws Exception {
		reload();
	}

	@Override
	public void reload() throws Exception {
//		initPkey();
		BaseZXService.setConfig(this.copy());
	}

	protected ZXConfig copy() throws CloneNotSupportedException {
		ZXConfig newConfig = new ZXConfig();
		newConfig.setSystemId(systemId);
//		newConfig.setUserId(userId);
		newConfig.setKey(key);
		newConfig.setValidDay(validDay);
//		newConfig.setpKey(pKey);
		return newConfig;
	}

//	private void initPkey() {
//		/* pKey
//		      通过组合key+,+systemId+,+userId得到字符串
//		      通过字符串md5加密生成byte数组
//		      对这个数组进行base64加密得到pKey。
//		 */
//		String temp = key + "," + systemId + "," + userId;
//		pKey = new String(encoder.encode(DigestUtils.md5Digest(temp.getBytes(Charset.forName("UTF-8")))));
//	}

	@DisconfFileItem(name = "zx.systemId", associateField = "systemId")
	public String getSystemId() {
		return systemId;
	}
	//	@DisconfFileItem(name = "zx.userId", associateField = "userId")
//	public String getUserId() {
//		return userId;
//	}
	@DisconfFileItem(name = "zx.key", associateField = "key")
	public String getKey() {
		return key;
	}
	@DisconfFileItem(name = "zx.validDay", associateField = "validDay")
	public String getValidDay() {
		return validDay;
	}

//	public String getpKey() {
//		return pKey;
//	}

	public void setSystemId(String systemId) {
		this.systemId = systemId;
	}
	//	public void setUserId(String userId) {
//		this.userId = userId;
//	}
	public void setKey(String key) {
		this.key = key;
	}
	public void setValidDay(String validDay) {
		this.validDay = validDay;
	}
//	public void setpKey(String pKey) {
//		this.pKey = pKey;
//	}
}
